home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11121 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.misc,comp.lang.c,comp.lang.pl1,comp.lang.apl
  4. Subject: Re: GOTO controversy
  5. Date: 21 Mar 1996 16:16:58 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4isrhqINNpol@keats.ugrad.cs.ubc.ca>
  8. References: <314FB5F5.259B@simi.is> <3151B47F.70FD@connix.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <3151B47F.70FD@connix.com>,
  12. Scott Hawley  <shawley@connix.com> wrote:
  13. >What to you think? Loops without using loops?
  14. >
  15. >a loop:
  16. >
  17. >for(i=0;i<10;i++)
  18. >{
  19. >  stuff
  20. >}
  21. >
  22. >Looping with out a loop
  23. >
  24. >no_loop(0,10);
  25. >
  26. >no_loop(int start, int end)
  27. >{
  28. >    if(start != end) {
  29. >        stuff
  30. >        no_loop(start+1,end);
  31.  
  32. recursion! ouch! I'd hate to do a no_loop(1,1000000000);
  33.  
  34. >    }
  35. >}
  36. >
  37. >I haven't tested this but you get the idea.
  38.  
  39. How do you intend to pass ``stuff'' to the no_loop function? I think you are a
  40. little confused about C! :)
  41.  
  42. For this to be of any practical use, one should be able to supply an arbitrary
  43. statement block in the place of stuff. One way to do this is to define no_loop
  44. as a pre-processor macro. This has been done since the dawn of C for creating
  45. idioms for traversing linked list and other data structures. I have recently
  46. seen a program written in 1980 which contains an example.
  47.  
  48.  
  49.     #define no_loop(S,E) for (;(S) < (E);(S)++)
  50.  
  51. You then do:
  52.  
  53.     int x, y = 4;
  54.  
  55.     no_loop(x, y) {
  56.         printf("%i\n",x);
  57.     }
  58.  
  59. The first argument to no_loop must be an lvalue, and it gets incremented, so
  60. this does not have the semantics you were looking for in your own definition of
  61. the no_loop() function.
  62.  
  63. How this is better than using an explicit for() loop is not clear to me. Such a
  64. macro is useful when you have to traverse some complex data structure. In my
  65. own hash-table module, I define a macro called 'hash_for', which will step
  66. through all the elements inserted into the hash table and set the specified
  67. node pointer to each one in turn. The reason I have this is because the hash
  68. table is somewhat involved. There have to be two nested loops to do the
  69. traversal. The outer one steps through the array of chains, and the inner one
  70. traverses any non-empty chain. Written explicitly, the code looks ugly and
  71. would confound any program that has to many such traversals in various places.
  72.  
  73. Instead, all I have to do is:
  74.  
  75.     hash_for(hash_table_ptr, data_ptr) {
  76.         /* process data_ptr */
  77.     } hash_rof;
  78.  
  79.  
  80. -- 
  81.  
  82.